﻿using System;
using HttpApiGateway.Interface;
using RpcCacheClient.Interface;

namespace HttpApiGateway.Idempotent.Token
{
    [RpcClient.Attr.UnityName("Redis")]
    [RpcClient.Attr.ClassLifetimeAttr(RpcClient.Attr.ClassLifetimeType.单例)]
    internal class RedisKeyService : IKeyIdempotent
    {
        private readonly IRedisCache _Redis;
        private readonly IIdempotentConfig _Config;
        public RedisKeyService(IRedisCache redis, IIdempotentConfig config)
        {
            _Redis = redis;
            _Config = config;
        }
        public StatusSaveType SaveType => StatusSaveType.Redis;
        public void Dispose()
        {

        }
        public bool SubmitToken(string tokenId)
        {
            string key = "Idempotent_" + tokenId;
            return _Redis.Add(key, tokenId, new TimeSpan(0, 0, _Config.Expire));
        }
    }
}
